define(['angularAMD'], function (angularAMD) {
    angularAMD.service('B0226Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$calculater',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater) {
            this.init = function(options) {
                options.type = options.type||'';
                function equipment_switch_info_get_json(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.plot_equipment_switch_info_get',
                        content : content,
                        //msg : 'B0226.msg.equipment_switch_info_get',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }
                //20210125 add by德義益暉 for#80098
                function get_equipment_check_result(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.equipment_check_result',
                        content : content,
                        success : function(data){
                            var errorMsg = '', warningMsg = '';
                            if(data.equipment_check_up == 2 ){
                                errorMsg += data.warning_msg ;
                            } else if(data.equipment_check_up == 1 ){
                                warningMsg += data.warning_msg ;
                            }
                            if(errorMsg != ''){
                                $rootScope.showAlert(errorMsg);
                                if(callback){
                                    callback(data);
                                }
                            } 
                            else if(warningMsg != ''){
                                $rootScope.showConfirm(warningMsg, function(confirmDialog){
                                    confirmDialog.hide();
                                    if(callback){
                                        callback(data);
                                    }
                                });
                            }
                            else if(callback){
                                callback(data);
                            }   
                        }
                    });
                }   

                function equipment_switch_process_json(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.equipment_switch_process',
                        content : content,
                        //msg : 'B0226.msg.equipment_switch_process',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                function validate(equipment_detail){
                    var msg = '';
                    if(equipment_detail.length != 0){
                        for(var i=0;i<equipment_detail.length;i++){
                            if(equipment_detail[i].qty == 0){
                                msg += $filter('translate')('B0226.msg.qty_zero'+options.type, equipment_detail[i])+'<br>';
                            }
                        }
                    } else {
                        msg += $filter('translate')('B0226.msg.no_equipemnt'+options.type);
                    }
                    return msg;
                }

                function openDialog(plotItem){
                    $mdDialog.dialog(
                        'module/moduleB/program/B02/B0226/B0226.html',
                        function (dialog) {
                            return {
                                css_url:'module/moduleB/program/B02/B0226/B0226.css',
                                type:options.type,
                                plotItem:plotItem,
                                equipment_detail:[],
                                back:function(){
                                    dialog.hide();
                                },
                                add:function(){
                                    $rootScope.showQueryList({
                                        key:'equipment_no',
                                        //mode:'M',
                                        header : [
                                            {label: $filter('translate')('B0226.list'+options.type+'.column1'), code:'equipment_no'},
                                            {label: $filter('translate')('B0226.list'+options.type+'.column2'), code:'equipment_name'}
                                        ],
                                        query: function(conditions, feedback){
                                            feedback($filter('filter')(dialog.plotItem.equipment_list, {isSelect:'!Y'}));
                                            //feedback(dialog.plotItem.equipment_list);
                                        },
                                        selectItem:dialog.equipment_detail,
                                        confirm: function(selectItem, queryDialog){
                                            //20210125 add by德義益暉 for#80098
                                            var content = {
                                                machine_no:selectItem.equipment_no
                                            };
                                            get_equipment_check_result(content, function(data){
                                                queryDialog.hide();
                                                var equipment_check_up = data.equipment_check_up
                                                if(equipment_check_up != '2'){
                                                    selectItem.isSelect = 'Y';
                                                    dialog.equipment_detail.push({
                                                        equipment_no:selectItem.equipment_no,
                                                        equipment_name:selectItem.equipment_name,
                                                        equipment_status:selectItem.equipment_status,
                                                        qty:0
                                                    });
                                                }
                                            })
                                            /*
                                            dialog.hide();
                                            dialog.equipment_detail = selectItem;
                                            */
                                        }
                                    });
                                },
                                remove:function(index, item){
                                    var temp = $filter('filter')(dialog.plotItem.equipment_list, {equipment_no:item.equipment_no})[0];
                                    temp.isSelect = undefined;
                                    dialog.plotItem.remain_qty = $calculater.decimalAdd(dialog.plotItem.remain_qty, item.qty);
                                    dialog.equipment_detail.splice(index, 1);
                                },
                                input_qty:function(event, item){
                                    var target = event.currentTarget;
                                    angular.element(target).addClass('is-focus');
                                    $calculater.open(function(num,calcDialog){
                                        angular.element(target).removeClass('is-focus');
                                        if((+dialog.plotItem.remain_qty)+(item.qty)-(+num) < 0){
                                            $rootScope.showAlert($filter('translate')('B0226.msg.gt_ori_qty'+options.type)+$calculater.decimalAdd(dialog.plotItem.remain_qty, item.qty));
                                        } else {
                                            dialog.plotItem.remain_qty = $calculater.decimalSub($calculater.decimalAdd(dialog.plotItem.remain_qty, item.qty),num);
                                            item.qty = (+num);
                                            calcDialog.hide();
                                        }
                                    },
                                    function(calcDialog){
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum : item.qty,
                                        decimal : true
                                    });
                                },
                                confirm:function(){
                                    //檢核輸入0則不可確認
                                    var msg = validate(dialog.equipment_detail);
                                    if(msg == ''){
                                        equipment_switch_process_json({
                                            switch_info:[{
                                                lot_no:dialog.plotItem.lot_no,
                                                op_no:dialog.plotItem.op_no,
                                                equipment_no:dialog.plotItem.cur_equipment_no,
                                                remain_qty:dialog.plotItem.remain_qty,
                                                lot_stamp:dialog.plotItem.lot_stamp,
                                                //20220822 modify by 翰鴻 for M#105814
                                                equipment_detail: dialog.equipment_detail,
                                                switch_plot_state: options.switch_plot_state
                                            }]
                                        }, function(){
                                            dialog.hide();
                                            $rootScope.showToast($filter('translate')('B0226.msg.success'));
                                            if(options.callback)
                                                options.callback();
                                        });
                                    } else {
                                        $rootScope.showAlert(msg);
                                    }
                                }
                            };
                        }
                    );
                }

                (function(){
                    var content = {
                        lot_no:options.plotItem.plot_no,
                        op_no:options.plotItem.op_no,
                        //20220822 modify by 翰鴻 for M#105814
                        equipment_no: options.machine_no,
                        switch_plot_state: options.switch_plot_state
                    };
                    equipment_switch_info_get_json(content, function(data){
                        if (options.switch_plot_state == 2)
                            data.remain_qty = options.plotItem.qty;
                        else
                            data.remain_qty = data.cur_qty;
                        openDialog(data);
                    });
                })();
            };
        }
    ]);
});
